/**
 * 
 */
package com.jolestar.yayaweibotong.dao;

import java.util.Date;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.jolestar.yayaweibotong.ServiceType;

/**
 * @author jolestar@gmail.com
 * 
 */
public class InviteTokenDomain extends AbstractDomain {

	/**
	 * 
	 */
	private static final long serialVersionUID = -433046970076740297L;

	public static final String USER_PROPERTY = "user";
	public static final String USED_USER_PROPERTY = "used_user";
	public static final String USED_PROPERTY = "used";
	public static final String USED_DATE = "used_date";

	/**
	 * @param serviceType
	 * @param originalId
	 */
	public InviteTokenDomain(String originalId, UserDomain user) {
		super(ServiceType.yaya, originalId);
		this.set(USER_PROPERTY, user.getKey());
		this.set(USED_PROPERTY, false);
	}

	/**
	 * @param entity
	 */
	public InviteTokenDomain(Entity entity) {
		super(entity);
	}

	/**
	 * token创建用户
	 * 
	 * @return
	 */
	public UserDomain getUser() {
		Key userkey = this.get(USER_PROPERTY);
		return DomainFactory.load(userkey);
	}

	public void use(UserDomain user) {
		this.set(USED_USER_PROPERTY, user.getKey());
		this.set(USED_PROPERTY, true);
		this.set(USED_DATE, new Date());
	}

	/**
	 * token 使用用户
	 * 
	 * @return
	 */
	public UserDomain getUsedUser() {
		Key userkey = this.get(USED_USER_PROPERTY);
		return DomainFactory.load(userkey);
	}

	public boolean isUsed() {
		return this.get(USED_PROPERTY, false);
	}

	public Date getUsedDate() {
		return this.get(USED_DATE);
	}
}
